package net.posick.ws.xml;

import java.io.Serializable;

//import net.posick.ws.Constants;

public class Namespace implements Serializable
{
    private static final long serialVersionUID = 201403191301L;
    
//    protected static final String LOG_TAG = Constants.LOG_TAG + "." + Name.class.getSimpleName();
    
    public static final String NAMESPACE_ATTRIBUTE_KEY = "xmlns";
    
    protected static final int NAMESPACE_ATTRIBUTE_KEY_LENGTH = NAMESPACE_ATTRIBUTE_KEY.length();
    
    protected String namespaceUri;
    
    protected String prefix;
    
    protected String string = null;
    
    protected Integer hashCode = null;
    
    
    public Namespace(String prefix, String namespaceUri)
    {
        super();
        this.namespaceUri = namespaceUri;
        
        if (prefix != null)
        {
            if (prefix.startsWith(NAMESPACE_ATTRIBUTE_KEY))
            {
                int prefixLength = prefix.length();
                if (NAMESPACE_ATTRIBUTE_KEY.length() == prefixLength)
                {
                    this.prefix = null;
                } else
                {
                    if (prefix.charAt(NAMESPACE_ATTRIBUTE_KEY_LENGTH) == ':' && prefixLength > NAMESPACE_ATTRIBUTE_KEY_LENGTH)
                    {
                        this.prefix = prefix.substring(NAMESPACE_ATTRIBUTE_KEY_LENGTH + 1);
                    } else
                    {
                        this.prefix = null;
                    }
                }
            } else
            {
                this.prefix = prefix;
            }
        } else
        {
            this.prefix = null;
        }
    }


    public String getUri()
    {
        return namespaceUri;
    }


    public void setNamespaceUri(String namespaceURI)
    {
        this.namespaceUri = namespaceURI;
    }


    public String getPrefix()
    {
        return prefix;
    }


    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
    }
    
    
    @Override
    public int hashCode()
    {
        if (hashCode == null)
        {
            hashCode = Integer.valueOf(toString().hashCode());
        }
        
        return hashCode.intValue();
    }
    
    
    @Override
    public String toString()
    {
        if (string == null)
        {
            string = (prefix != null && prefix.length() > 0 ? NAMESPACE_ATTRIBUTE_KEY + ":" + prefix : NAMESPACE_ATTRIBUTE_KEY) + "=\"" + namespaceUri + "\"";
        }
        
        return string;
    }
    
    
    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        } else if (o instanceof Namespace)
        {
            Namespace that = (Namespace) o;
            
            String thisPrefix = this.prefix;
            String thisNamespaceUri = this.namespaceUri;
            String thatPrefix = that.prefix;
            String thatNamespaceUri = that.namespaceUri;
            
            if (thisPrefix == null || thatPrefix == null || thisPrefix == thatPrefix || thisPrefix.equals(thatPrefix))
            {
                if (thisNamespaceUri == null || thatNamespaceUri == null || thisNamespaceUri == thatNamespaceUri || thisNamespaceUri.equals(thatNamespaceUri))
                {
                    return true;
                }
            }
        }
        
        return false;
    }


    public boolean hasPrefix()
    {
        return this.prefix != null;
    }


    public boolean hasUri()
    {
        return this.namespaceUri != null && this.namespaceUri.length() > 0;
    }
}
